home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 February / macformat-047.iso / Shareware Plus / Developers / DLOGManager 1.02 / Source Code / GeneralUtilities.cp < prev    next >
Encoding:
Text File  |  1996-09-16  |  3.8 KB  |  234 lines  |  [TEXT/KAHL]

  1. /*****
  2.  *    GeneralUtilities.c
  3.  *
  4.  *    Routines di utilità generale.
  5.  *    © Com&Media, 1994
  6.  *
  7.  *    15/10/94    Scorporo da Utilities.h (Fabio Barbieri)
  8.  *    18/8/94        Prima stesura (Fabio Barbieri)
  9.  *
  10.  *
  11.  *****/
  12.  
  13.  
  14. /*****    Include standard        *****/
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <limits.h>
  20. #include <Events.h>
  21. #include <Fonts.h>
  22. #include <Memory.h>
  23. #include <TextUtils.h>
  24.  
  25. /*****    Include locali            *****/
  26. #include "GeneralUtilities.h"
  27.  
  28. /*****    Define macro            *****/
  29.  
  30. /*****    Define valori            *****/
  31.  
  32. #pragma segment GeneralUtilities
  33.  
  34. /*****    Typedef globali            *****/
  35.  
  36. /*****    Funzioni esterne        *****/
  37.  
  38. /*****    Variabili esterne        *****/
  39.  
  40. /*****    Variabili globali        *****/
  41.  
  42. /*****    Statiche globali        *****/
  43.  
  44. /*****    Function prototyping    *****/
  45.  
  46.  
  47. /****
  48.  *    Wait(howMuch)
  49.  *
  50.  *    Attende <howMuch> sessantesimi di secondo.
  51.  *
  52.  ****/
  53.  
  54. void Wait(short    howMuch)
  55. {
  56.     long stopTime = TickCount() + (long)howMuch;
  57.     while(TickCount() <= stopTime);
  58. }
  59. /* end Wait */
  60.  
  61. #ifdef PIPPO
  62. /****
  63.  *    FixedToFloat(valFixed, valFloat)
  64.  *
  65.  *    Converte il valore Fixed <valFixed> nel valore float <valFloat>.
  66.  *
  67.  ****/
  68.  
  69. void    FixedToFloat(Fixed    valFixed, float    *valFloat)
  70. {
  71.     short            parteInt, i;
  72.     unsigned long    mask;
  73.     float            valoreBit, parteFrac;
  74.     
  75.     parteInt = HiWord(valFixed);
  76.     
  77.     valoreBit = 0.5;
  78.     mask = 0x8000L;
  79.     parteFrac = 0.0;
  80.     for(i = 0; i < 16; i++)
  81.     {
  82.         if(valFixed & mask)
  83.             parteFrac += valoreBit;
  84.         mask = mask >> 1;
  85.         valoreBit *= 0.5;
  86.     }
  87.     *valFloat = parteFrac + (float)parteInt;
  88. }
  89. /* end FixedToFloat */
  90.  
  91.  
  92. /****
  93.  *    FloatToFixed(valFloat, valFixed)
  94.  *
  95.  *    Converte il valore float <valFloat> nel valore Fixed <valFixed>.
  96.  *
  97.  ****/
  98.  
  99. void    FloatToFixed(float    valFloat, Fixed    *valFixed)
  100. {
  101.     char    buffer[256], *pFrac;
  102.     short    parteInt, parteFrac, divisore, i;
  103.  
  104.     divisore = 10000;
  105.     sprintf(buffer, "%.4f", valFloat);
  106.     for(i = 0; buffer[i] != '.'; i++)
  107.         if(buffer[i] == '\0')
  108.             break;
  109.     if(buffer[i] == '\0')
  110.         parteFrac = 0;
  111.     else
  112.     {
  113.         pFrac = &buffer[i+1];
  114.         parteFrac = atoi(pFrac);
  115.     }
  116.     
  117.     buffer[i] = '\0';
  118.     parteInt = atoi(buffer);
  119.     *valFixed = (((long)parteInt) << 16) | FixRatio(parteFrac, divisore);
  120. }
  121. /* end FloatToFixed */
  122. #endif
  123.  
  124. /****
  125.  *    GetFontNumber(fontName, fontNum)
  126.  *
  127.  *    Ricerca la font di nome <fontName> tra quelle presenti nel sistema; se la trova, ne pone l'id
  128.  *    in <fontNum> e torna true, altrimenti pone <fontNum> a 0 e torna false.
  129.  *
  130.  ****/
  131.  
  132. Boolean    GetFontNumber(Str255    fontName, short    *fontNum)
  133. {
  134.     Str255     systemFontName;
  135.  
  136.     GetFNum(fontName, fontNum);
  137.     if(*fontNum == 0)
  138.     {
  139.         GetFontName(0, systemFontName);
  140.         return(EqualString(fontName, systemFontName, false, false));
  141.     }
  142.     else
  143.         return(true);
  144. }
  145. /* end GetFontNumber */
  146.  
  147.  
  148. /****
  149.  *    CompHandle(hdl1, hdl2)
  150.  *
  151.  *    Confronta il contenuto degli handle <hdl1> e <hdl2>: torna true se sono identici,
  152.  *    false altrimenti.
  153.  *
  154.  ****/
  155.  
  156. Boolean    CompHandle(Handle hdl1, Handle hdl2)
  157. {
  158.     short    len;
  159.     short    val;
  160.     
  161.     if(hdl1 || hdl2)
  162.     {
  163.         if(hdl1 == nil || hdl2 == nil)
  164.             return(false);
  165.             
  166.         len = GetHandleSize(hdl1);
  167.         if(len != GetHandleSize(hdl2))
  168.             return(false);
  169.         
  170.         HLock(hdl1);
  171.         HLock(hdl2);
  172.         val = memcmp(*hdl1, *hdl2, len);
  173.         HUnlock(hdl1);
  174.         HUnlock(hdl2);
  175.         return(val == 0);
  176.     }
  177.     return(true);
  178. }
  179. /* end CompHandle */
  180.  
  181.  
  182. /****
  183.  *    CicloAttesa(void)
  184.  *
  185.  *    Richiama WaitNextEvent per non bloccare le altre applicazioni.
  186.  *
  187.  ****/
  188.  
  189. void    CicloAttesa(void)
  190. {
  191.     EventRecord        event;
  192.  
  193.     WaitNextEvent(everyEvent, &event, LONG_MAX, nil);
  194. }
  195. /* end CicloAttesa */
  196.  
  197. /***
  198.  *
  199.  *    get the lower byte of a short.
  200.  *
  201.  ****/
  202. short getLow(short value)
  203. {
  204.     return(value & 0xFF);
  205. }
  206. /* end getLow */
  207.  
  208. /***
  209.  *
  210.  *    get the lower byte of a short.
  211.  *
  212.  ****/
  213. short getHi(short value)
  214. {
  215.     short val = value & 0xFF00;
  216.     
  217.     return(val >> 8);
  218. }
  219. /* end getHi */
  220.  
  221. /***
  222.  *
  223.  *    merge the high and low part of a short.
  224.  *
  225.  ***/
  226. short merge(short hi, short low)
  227. {
  228.     short val = hi & 0xFF;
  229.     
  230.     val = val << 8;
  231.     return(val | (low & 0xFF));
  232. }
  233. /* end merge */
  234.